package com.itkaige.boot.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class DruidConfig {

    @ConfigurationProperties(prefix="spring.datasource")
    @Bean
    public DruidDataSource f(){
        return  new DruidDataSource();
    }

//    后台控制

    @Bean
    public ServletRegistrationBean<StatViewServlet> statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean =new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
        HashMap<String,String> initPar=new HashMap<>();
        initPar.put("loginUsername","admin");
        initPar.put("loginPassword","123456");
//        initPar.put("allow","");
        bean.setInitParameters(initPar);
        return bean;

    }

//    过滤器filter
    public FilterRegistrationBean webStatFilter(){
        FilterRegistrationBean bean =new FilterRegistrationBean();
        bean.setFilter(new WebStatFilter());
//       过滤请求
        Map<String,String> initPar=new HashMap<>();
//        这些东西不进行统计
        initPar.put("exclusion","*.js,*.css,/druid/*");
        bean.setInitParameters(initPar);
        return bean;
    }
}
